﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Miner.UI.Controls.Interfaces;
using Miner.Model.Logic;

namespace Miner.UI.Controls
{
    public partial class DrawAreaCtrl : UserControl, IDrawAreaCtrl
    {
        private PictureBox drawArea = new PictureBox();

        public DrawAreaCtrl()
        {
            InitializeComponent();

            this.drawArea.SizeMode = PictureBoxSizeMode.StretchImage;
            this.Controls.Add(drawArea);
            this.drawArea.MouseUp+=OnBlockClicked;
        }

        public void Init(GameOptions opt, int width, int height)
        {
            this.SuspendLayout();

            int newWidth = width;
            int newHeight = height;

            if (width <= opt.xBlocksCount * opt.BlockSize)
            {
                newWidth = opt.xBlocksCount * opt.BlockSize;
            }
            if(height <= opt.yBlocksCount * opt.BlockSize)
            {
                newHeight = opt.yBlocksCount * opt.BlockSize;
            }
            this.Size = new Size(newWidth, newHeight);
            
            this.MinimumSize = this.Size;
            this.drawArea.Size = new Size(opt.xBlocksCount * opt.BlockSize, opt.yBlocksCount * opt.BlockSize);
            this.drawArea.Location = new Point(this.Size.Width / 2 - drawArea.Size.Width / 2, this.Size.Height / 2 - drawArea.Size.Height / 2);
            this.drawArea.MinimumSize = drawArea.Size;

            this.ResumeLayout(true);
        }

        private void OnBlockClicked(object sender, MouseEventArgs e)
        {
            if(this.BlockClicked!=null)
            {
                this.BlockClicked(sender, e);
            }
        }

        public Image Image
        {
            get
            {
                return this.drawArea.Image;
            }
            set
            {
                this.drawArea.Image = value;
            }
        }

        public event MouseEventHandler BlockClicked;


        public void Lock()
        {
            this.Enabled = false;
        }

        public void UnLock()
        {
            this.Enabled = true;
        }
    }
}
